package com.a.lib_ui.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import com.a.lib_ui.R;
import com.a.lib_ui.bean.ChildClassType;
import com.a.lib_ui.bean.Packaging;
import com.a.lib_ui.fragment.BigPictureFragment;
import com.a.lib_ui.fragment.BottomWindowsFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 该类主要来实现弹出框
 */
public class PopWindowsActivity extends AppCompatActivity {
    private static final String TAG = PopWindowsActivity.class.getSimpleName();
    private int type;
    private List<ChildClassType> childTypeList;
    private List<Packaging> packagingList;

    private String url;
    //创建一个基类Fragment
    Fragment baseFragment;

    private WindowManager.LayoutParams lp;
    private Window dialogWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_popwindow);
        setFinishOnTouchOutside(true);
        //计算弹窗显示的大小
        dialogWindow = getWindow();
        lp = dialogWindow.getAttributes(); // 获取对话框当前的参数值

        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //加载数据
        getData();

        //添加Fragment
        addFragment();
    }

    private void getData() {
        Bundle bundle = getIntent().getExtras();

        if (bundle != null) {
            type = bundle.getInt("key", 1);//默认为1 按需求可以任意写值（可以不写，系统默认为0）
        }


        //商品的筛选
        if (type == 2) {
            lp.gravity = Gravity.BOTTOM;
            dialogWindow.setAttributes(lp);
            ArrayList list = (ArrayList) bundle.getSerializable("childTypeList");
            Log.d(TAG, "list===size======+" + list.size() + "+==+++++" + list.get(0).toString());
            childTypeList = (List<ChildClassType>) list.get(0);
            packagingList = (List<Packaging>) list.get(1);
        }


        if (type == 3) {  //图片放大
            lp.gravity = Gravity.CENTER;
            setContentView(R.layout.activity_big_picture);
            url = (String) bundle.get("url");
        }

        if (type == 4) {
            lp.gravity = Gravity.BOTTOM;
            setContentView(R.layout.activity_big_picture);
        }

    }

    private void addFragment() {//这里显示fragment样式
        Log.i(TAG, "addFragment: " + type);
        //通过动态配置参数，替换Fragment显示
        switch (type) {
            case 1:
                // baseFragment = new SearchFragment();
                break;
            case 2:
                baseFragment = BottomWindowsFragment.newInstance(childTypeList, packagingList);
                break;
            case 3:
                baseFragment = BigPictureFragment.newInstance(url);
                break;
        }
        getSupportFragmentManager().beginTransaction().replace(R.id.body, baseFragment).commit();
    }

}
